// Decompiled by Jad v1.5.7f. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   ExamAction.java

package com.iphaeton.lms.action.exam;

import com.iphaeton.common.helper.Pagination;
import com.iphaeton.common.helper.UserSession;
import com.iphaeton.exception.BusinessException;
import com.iphaeton.lms.business.certificate.ICertificateService;
import com.iphaeton.lms.business.exam.IExamService;
import com.iphaeton.lms.business.exam.IQuestionService;
import com.iphaeton.lms.pojo.certificate.Certificate;
import com.iphaeton.lms.pojo.exam.*;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import org.apache.log4j.Logger;
import org.apache.struts.action.*;

// Referenced classes of package com.iphaeton.lms.action.exam:
//            ExamBizSupportAction

public class ExamAction extends ExamBizSupportAction
{

    public ExamAction()
    {
    }

    public ActionForward queryExam(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        List list = null;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String user_id = userSession.getId();
            String organize_url = userSession.getOrgUrl();
            list = exam.queryCurrentExam(user_id, organize_url);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("list", list);
        return mapping.findForward("my_exam");
    }

    public ActionForward examStart(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String type = request.getParameter("type");
        Calendar cal = Calendar.getInstance();
        java.util.Date time = cal.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = df.format(time);
        String map = "";
        if(type.equals("1"))
            map = "exam_start_one";
        else
        if(type.equals("2"))
            map = "exam_start_all";
        String id = request.getParameter("id");
        List list = null;
        Paper p = null;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String user_id = userSession.getId();
            p = question.findPaperById(id);
            list = exam.queryExamQuestion(id);
            exam.startExam(id, user_id, currentTime);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            mapping.findForward("session_dead");
        }
        request.setAttribute("paper", p);
        request.setAttribute("list", list);
        return mapping.findForward(map);
    }

    public ActionForward handPaper(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String id = request.getParameter("id");
        List list = null;
        Paper p = null;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String user_id = userSession.getId();
            p = question.findPaperById(id);
            if(p.getSort().intValue() == 5)
            {
                Certificate c = question.findPaperCertificate(id);
                certificate.setCertificateUser(c.getId(), user_id);
            }
            list = exam.queryExamQuestion(id);
            for(int i = 0; i < list.size(); i++)
            {
                String question_answer = "";
                String question_id = ((String) (((Map)list.get(i)).get("id")));
                Question q = question.findQuestionById(question_id);
                if(q.getSort().getName().equals("\u591A\u9879\u9009\u62E9\u9898"))
                {
                    String temp[] = request.getParameterValues(question_id);
                    for(int j = 0; temp != null && j < temp.length; j++)
                        question_answer = question_answer + temp[j];

                } else
                {
                    question_answer = request.getParameter(question_id);
                }
                if(question_answer == null)
                    question_answer = "";
                exam.studentAnswer(id, user_id, question_id, question_answer);
            }

        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("paper", p);
        return mapping.findForward("exam_hand_ok");
    }

    public ActionForward paperMark(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String paper_id = request.getParameter("paperId");
        String user_id = request.getParameter("userId");
        List list = null;
        Paper p = null;
        try
        {
            list = exam.paperMark(paper_id, user_id);
            p = question.findPaperById(paper_id);
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("paper", p);
        request.setAttribute("user_id", user_id);
        request.setAttribute("list", list);
        return mapping.findForward("paper_mark");
    }

    public ActionForward markComplete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String paperId = request.getParameter("paperId");
        String userId = request.getParameter("userId");
        String total_score = request.getParameter("total_score");
        Paper p;
        try
        {
            p = question.findPaperById(paperId);
            if(p.getSort().intValue() == 5)
            {
                Certificate cert = question.findPaperCertificate(paperId);
                if(cert.getCondition().floatValue() <= Float.parseFloat(total_score))
                    certificate.userCertificateUpdate(cert.getId(), userId, 1);
                else
                    certificate.userCertificateUpdate(cert.getId(), userId, 2);
            }
            List list = exam.queryExamQuestion(paperId);
            for(int i = 0; i < list.size(); i++)
            {
                logger.info("\u7B2C" + i + "\u9898\u5F97\u5206:" + request.getParameter("score_" + i));
                exam.setQuestionScore(paperId, userId, ((String) (((Map)list.get(i)).get("id"))), Float.parseFloat(request.getParameter("score_" + i)));
            }

            logger.info("\u8BD5\u5377\u603B\u5206:" + total_score);
            exam.setPaperScore(paperId, userId, Float.parseFloat(total_score));
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        request.setAttribute("paper", p);
        return mapping.findForward("paper_mark_ok");
    }

    public ActionForward myPaper(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        String paper_id = request.getParameter("id");
        String user_score = null;
        List list;
        Paper p;
        try
        {
            UserSession userSession = (UserSession)request.getSession().getAttribute("userSession");
            String user_id = userSession.getId();
            p = question.findPaperById(paper_id);
            list = exam.myPaper(paper_id, user_id);
            user_score = (String)((Map)list.get(0)).get("user_score");
        }
        catch(BusinessException e)
        {
            e.printStackTrace();
            request.setAttribute("exception", e);
            return mapping.findForward("failure");
        }
        catch(NullPointerException ne)
        {
            return mapping.findForward("session_dead");
        }
        request.setAttribute("paper", p);
        request.setAttribute("list", list);
        request.setAttribute("score", user_score);
        return mapping.findForward("my_paper");
    }

    private static Logger logger;
    private DynaActionForm daf;
    private Pagination pageInfo;
    static Class class$0; /* synthetic field */

    static 
    {
        logger = Logger.getLogger(com.iphaeton.lms.action.exam.ExamAction.class.getName());
    }
}
